package com.jwdf.uda.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.event.IndividualEvent;
import com.jwdf.uda.event.ThirdPartEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.BindQqAccountAction;
import com.jwdf.uda.http.action.BindWechatAccountAction;
import com.jwdf.uda.http.action.BindWeiboAccountAction;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.ui.fragment.user.personal.IndividualInfoFragment;
import com.jwdf.uda.utils.EventBusUtil;

import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/6/21.
 */
public class IndividualActivity extends AbsTitleBarActivity {

    public static void start(Context context, PbBean.PbUserInfo info) {
        Intent intent = new Intent(context, IndividualActivity.class);
        intent.putExtra("user_info", info);
        context.startActivity(intent);
    }

    @Override
    protected String getTopTitle() {
        return getString(R.string.user_individual);
    }

    @Override
    protected BaseFragment onCreateContentFragment() {
        return new IndividualInfoFragment();
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBusUtil.registerEvent(this);
    }
    @Override
    protected Bundle onPutArguments() {
        Bundle args = new Bundle();
        args.putSerializable("user_info", getIntent().getSerializableExtra("user_info"));
        return args;
    }
    @Override
    public void onDestroy() {
        EventBusUtil.unregisterEvent(this);
        super.onDestroy();
    }

    public void onEvent(IndividualEvent event) {
        if (event.getEvent() == IndividualEvent.USER_SAVE_INFO_EVENT) {
            finish();
        }
    }

    public void onEvent(ThirdPartEvent event) {
        if (ThirdPartEvent.EVENT_AUTH_COMPLETE == event.getEvent()) {
            if (Wechat.NAME.equals(event.loginType)) {
                bindWechatAccount(event.usrOpenId);
            }else if(QQ.NAME.equals(event.loginType)){
                bindQqAccount(event.usrOpenId);
            } else {
                bindWeiboAccount(event.usrOpenId);
            }
        }
    }

    private void bindWechatAccount(String openId) {
        onNewRequestCall(BindWechatAccountAction.newInstance(IndividualActivity.this, openId).request(new AHttpService.IResCallback<PbBean.PbResUserBindWeChatAccount>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResUserBindWeChatAccount> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (HttpHelper.showError(IndividualActivity.this, resultCode, baseServerInfo, getString(R.string.error_net))) {
                    EventBusUtil.post(new ThirdPartEvent(ThirdPartEvent.EVENT_CLOSE_PROGRESS));
                    return;
                }
                IndividualEvent event = new IndividualEvent(IndividualEvent.USER_WECHAT_EVENT);
                EventBusUtil.post(event);
            }
        }));
    }

    private void bindWeiboAccount(String openId) {
        onNewRequestCall(BindWeiboAccountAction.newInstance(IndividualActivity.this, openId).request(new AHttpService.IResCallback<PbBean.PbResUserBindWeiboAccount>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResUserBindWeiboAccount> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (HttpHelper.showError(IndividualActivity.this, resultCode, baseServerInfo, getString(R.string.error_net))) {
                    EventBusUtil.post(new ThirdPartEvent(ThirdPartEvent.EVENT_CLOSE_PROGRESS));
                    return;
                }
                IndividualEvent event = new IndividualEvent(IndividualEvent.USER_WEIBO_EVENT);
                EventBusUtil.post(event);
            }
        }));
    }

    private void bindQqAccount(String openId) {
        onNewRequestCall(BindQqAccountAction.newInstance(IndividualActivity.this, openId).request(new AHttpService.IResCallback<PbBean.PbResUserBindQqAccount>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResUserBindQqAccount> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (HttpHelper.showError(IndividualActivity.this, resultCode, baseServerInfo, getString(R.string.error_net))) {
                    EventBusUtil.post(new ThirdPartEvent(ThirdPartEvent.EVENT_CLOSE_PROGRESS));
                    return;
                }
                IndividualEvent event = new IndividualEvent(IndividualEvent.USER_WEIBO_EVENT);
                EventBusUtil.post(event);
            }
        }));
    }

}
